package rm

import (
	"fmt"
	"strings"

	"gitee.com/eeppee_admin/go-tuiftp/reuse"
)

func Remove(p string) error {
	// path格式: [site]:[remote path]
	// p -> [站点名称]:[远程路径]
	arr := strings.Split(p, ":")
	// if len(arr) != 2 {
	// 	fmt.Println("远程路径格式错误,格式为 -> [站点名称]:[远程路径]")
	// 	os.Exit(1)
	// }
	sysConfig := &reuse.SysConfig{}
	if err := sysConfig.Read(); err != nil {
		return fmt.Errorf("error: rm cmd, Failed to read config error, %s", err)
	}
	for _, site := range sysConfig.Sites {
		if site.Name == arr[0] {
			client, err := reuse.NewFtpClient(site)
			if err != nil {
				return fmt.Errorf("error: rm cmd, Failed to connect: %s", err)
			}
			if err := client.Conn.Delete(arr[1]); err != nil {
				return fmt.Errorf("error: rm cmd, Failed to delete: %s", err)
			}
		}
	}
	fmt.Println("删除成功")
	return nil
}
